﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DataAccessLayer;
using GlobalObjects;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DataAccessLayerTests
{
    [TestClass]
    public class MongoTests
    {
        public string connectionString = Mongo.defaultConnectionString;
        public string db = Mongo.defaultDatabase;

        [TestMethod]
        public void deserialiseAndInsertDocument_DAL_valid()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //  string jsonString = "{ \"data\":[{\"temperature\":27.3,\"humidity\": 51.3,\"time\": \"21/07/2012 14:39:00\" },{\"temperature\":27.1,\"humidity\": 56.7 ,\"time\": \"21/07/2012 14:40:23\" }] }";
            //string jsonString = "{ \"data\":[{\"temperature\":28.3,\"humidity\": 54.3,\"time\": \"21/07/2013 15:06:00\" }] }";
            string jsonString = "{ \"data\":[{\"temperature\":35.3,\"humidity\": 54.3,\"time\": \"15/08/2013 15:07:00\" }] }";
            //Action
            mongo.deserialiseAndInsertDocument_DAL(jsonString, 3372, "temperature/Humidity Module");
            // userId 16
            //Assert
            // check the Database
        }
        [TestMethod]
        [ExpectedException(typeof(KeyNotFoundException))]
        public void deserialiseAndInsertDocument_DAL_invalidJson()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //  string jsonString = "{ \"data\":[{\"temperature\":27.3,\"humidity\": 51.3,\"time\": \"21/07/2012 14:39:00\" },{\"temperature\":27.1,\"humidity\": 56.7 ,\"time\": \"21/07/2012 14:40:23\" }] }";
            string jsonString = "{ \"invalid\":[{\"temperature\":28.3,\"humidity\": 54.3,\"time\": \"21/07/2013 15:06:00\" }] }";
            //Action
            mongo.deserialiseAndInsertDocument_DAL(jsonString, 1, "temperature/Humidity Module");
            //Assert
           
        }

        [TestMethod]
        public void removeDocumentById_DAL_valid()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            mongo.removeDocumentById_DAL("520107c239eab9208c9ca1bc", "temperature/Humidity Module");
            //Assert
        }
        [TestMethod]
        public void getDataPages_DAL_valid_complete() {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db); 
            //Action
            List<QueryEntity> list = mongo.getDataPages_DAL(1087, DateTime.Parse("22/07/2013 12:32:46"), DateTime.Parse("22/07/2013 12:41:06"), 10, "temperature");
            //Assert
            Assert.AreEqual(list.Count,10);
        }
        [TestMethod]
        public void getDataPages_DAL_valid_incomplete()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            List<QueryEntity> list = mongo.getDataPages_DAL(1, DateTime.Parse("01/01/2013 01:00:00").ToUniversalTime(), DateTime.Parse("30/12/2013 23:59:59").ToUniversalTime(), 2, "temperature");
            //Assert
            QueryEntity qe = list[list.Count - 1];
            Assert.AreEqual(qe.PageSize, 2);
        }

        [TestMethod]
        public void getTempAndHumadityByIdandTime_DAL_valid() {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            string json = mongo.getTempAndHumadityByIdandTime_DAL(2318, DateTime.Parse("22/07/2013 12:32:46"), DateTime.Parse("22/07/2013 12:41:06"), 200, "temperature");
            //Assert20-Jul-13 10:00:00
        }
        [TestMethod]
        public void getTempAndHumadityByIdandTime_DAL_invalidTime()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            string json = mongo.getTempAndHumadityByIdandTime_DAL(1, DateTime.Parse("20/07/2014 20:07:00"), DateTime.Parse("20/07/2014 20:10:50"), 2, "temperature");
            //Assert
            Assert.AreEqual(json,"[]");
        }
        [TestMethod]
        public void getTempAndHumadityByIdandTime_DAL_invalidId()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            string json = mongo.getTempAndHumadityByIdandTime_DAL(10, DateTime.Parse("20/07/2012 20:07:00"), DateTime.Parse("22/07/2012 20:10:50"), 2, "temperature");
            //Assert
            Assert.AreEqual(json, "[]");
        }

        //[TestMethod]
        //public void updateId_DAL_valid()
        //{
        //    //Arrange
        //    Mongo mongo = new Mongo(connectionString, db);
        //    //Action
        //    mongo.updateId_DAL(1089, 2320, "temperature");
        //}

        [TestMethod]
        public void getDataByIdandTime_DAL_valid()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            string json = mongo.getDataByIdandTime_DAL(2348, DateTime.Parse("12/08/2013 15:17:50"), DateTime.Parse("12/08/2013 15:20:18"), 200, "gps");
            //Assert

        }
        [TestMethod]
        public void getDataByIdandTime_DAL_invalidId()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            string json = mongo.getDataByIdandTime_DAL(348, DateTime.Parse("12/08/2013 15:17:50"), DateTime.Parse("12/08/2013 15:20:18"), 200, "gps");
            //Assert20-Jul-13 10:00:00
            Assert.AreEqual(json,"[]");
        }
        [TestMethod]
        public void getDataByIdandTime_DAL_invalidTime()
        {
            //Arrange
            Mongo mongo = new Mongo(connectionString, db);
            //Action
            string json = mongo.getDataByIdandTime_DAL(348, DateTime.Parse("12/08/2012 15:17:50"), DateTime.Parse("12/08/2012 15:20:18"), 200, "gps");
            //Assert20-Jul-13 10:00:00
            Assert.AreEqual(json, "[]");
        }
        [TestMethod]
        public void send_Email()
        {
            EmailSender email = new EmailSender();
            email.sendEmail("uclgadgeteer@gmail.com", "sensor id: " + 1061 + " has been trigerred", "Dear Pejman the lower limit of your sensor has been trigerred.");
        }
    }
}
